﻿@using SIGBugs.Shared.InputModels
@using Microsoft.AspNetCore.Blazor.Browser.Interop;
@using SIGBugs.Client.Libs;

@page "/login"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>登录</h1>

<hr />
<div class="row">
    <div class="col-md-4">
        <form>
            @if (!string.IsNullOrEmpty(errorMessage))
            {
                <div class="alert alert-danger">
                    <span class="oi oi-circle-x"></span>  @errorMessage
                </div>

            }

            <div class="form-group @(isInvalidName?"was-validated":"")">
                <label for="Name" class="control-label">用户名</label>
                <input for="Name" ref="inputName" class="form-control" bind="@im.Username" required />
                <div class="invalid-feedback">
                    请输入用户名
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Gender" class="control-label">密码</label>
                <input for="Password" ref="inputPassword" class="form-control" type="password" bind="@im.Password" required />

            </div>

            <div class="form-group">
                <button type="button" class="btn btn-primary" onclick="@(async () => await LoginSystem())">登录</button>
                <button class="btn" onclick="@cancel">取消</button>
            </div>
        </form>
    </div>
</div>


@functions {
    string errorMessage = string.Empty;
    bool isInvalidName = false;

    ElementRef inputName;
    ElementRef inputPassword;

    LoginIM im = new LoginIM();
    protected override void OnInit()
    {
        // RegisteredFunction.Invoke<bool>("log", "测试");
        base.OnInit();

    }
    protected override void OnAfterRender()
    {
        // RegisteredFunction.Invoke<bool>("tinymce", "textarea");
        inputName.Focus();
        base.OnAfterRender();

    }

    protected async Task LoginSystem()
    {
        if (string.IsNullOrEmpty(im.Username))
        {

            isInvalidName = true;
            inputName.Focus();
            return;

        }

        //   var result = await client.PostAsync(url, content);

        //  await Http.SendJsonAsync(HttpMethod.Post, "/api/Account", im);
        var content = new StringContent(JsonUtil.Serialize(im).ToString(), System.Text.Encoding.UTF8, "application/json");
        var result = await Http.PostAsync("/api/Account", content);

        if(result.StatusCode == System.Net.HttpStatusCode.OK)
        {
            string token = (await result.Content.ReadAsStringAsync());//result就是返回的结果。
            JsInterop.SetToken(token.Replace("\"",""),im.Username);

            JsInterop.SetHeaderStatus();

            UriHelper.NavigateTo("/");

            //var tokenjs = JsInterop.GetToken();
            //RegisteredFunction.Invoke<bool>("log", tokenjs);
        }
        else
        {
            errorMessage = "无效的用户名或密码。";
        }


        // Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJib2JAYW9sLmNvbSIsImp0aSI6ImZmNjhhMDA0LTlmNjYtNGIxNC05OTE4LWZmZWVhMDZiNTllYyIsImV4cCI6MTUwNTY5MTc1MiwiaXNzIjoiaHR0cDovL215Y29kZWNhbXAuaW8iLCJhdWQiOiJodHRwOi8vbXljb2RlY2FtcC5pbyJ9.CyD7nDAvlgt37C3wDcRG6DnlLOx3a7ih5UskL3xSUH0

        //Http.GetStringAsync(HttpMethod.Post, "/api/Account", im);
        // UriHelper.NavigateTo("/");
    }
    void cancel()
    {
        UriHelper.NavigateTo("/");
    }
}  